<template>
    <view class="safe-bottom">
      <view class="bg-white">
          <view class="u-p-y-50">
              <u-steps mode="number" :list="numList" :current="currentApplyStep" active-color="#FE572A"></u-steps>
          </view>
          <Step1 v-if="currentFormStep == 1 && flag" :logData="log_data" @submit="submit_1"></Step1>
          <Step2 v-if="currentFormStep == 2 && flag" :logData="log_data" @submit="submit_2" @back="previousStep"></Step2>
          <Step3 v-if="currentFormStep == 3 && flag" :logData="log_data" @submit="submit_3" @back="previousStep"></Step3>
          <Step4 v-if="currentFormStep == 4 && flag" :logData="log_data" @submit="submit_4" @back="previousStep"></Step4>
      </view>
    </view>
  </template>
  
  <script>
      import Step1 from './components/step1.vue';
      import Step2 from './components/step2.vue';
      import Step3 from './components/step3.vue';
      import Step4 from './components/step4.vue';
      import {MineCenterApi} from '@/api/serves.js'
      export default {
          components:{
              Step1,
              Step2,
              Step3,
              Step4
          },
          data() {
              return {
                  //审核流程
                  numList: [
                      {
                          name: '经营信息'
                      }, {
                          name: '执照信息'
                      }, {
                          name: '联系人信息'
                      }, {
                          name: '结算信息'
                      },
                  ],
                  //当前审核流程
                  currentApplyStep:-1,
                  //当前表单流程
                  currentFormStep:1,
                  //获取数据成功
                  flag:false,
				  //历史记录ID
				  id:'',
                  //历史数据
                  log_data:{},
                  //表单1
                  form1:{},
                  //表单2
                  form2:{}
              }
          },
          onLoad(){
              this._applyIndex()
          },
          methods:{
              submit_1(form_1){
                  this.form1 = form_1;
                  this.nextStep()
              },
              submit_2(form_2){
                  this.form2 = form_2;
                  this.nextStep()
              },
              submit_3(form_3){
                  this.form3 = form_3;
                  this.nextStep()
              },
              submit_4(form_4){
                  this.form4 = form_4;
                  this.submit_all()
              },
              nextStep(){
                  uni.pageScrollTo({
                      scrollTop: 0,
                      duration: 0
                  });
                  this.currentFormStep++;
                  this.currentApplyStep++;
              },
              submit_all(){
                  uni.showLoading()
                  let params = {}
                  Object.assign(params, uni.getStorageSync('applyForm_4'), uni.getStorageSync('applyForm_3'), uni.getStorageSync('applyForm_2'), uni.getStorageSync('applyForm_1'))
                  
                  //为了解决一些字段老版本为必须，新版本已移除，所以在此手动赋值
                  params.real_name = params.legal_name
                  params.id_card = params.legal_cert_no
                  params.card_no = params.legal_cert_no
				  params.merch_level = 1
				  params.id = this.id
  
                  let api = params.id ? 'applyEditXW' : 'applyAdd'
                  MineCenterApi[api](params).then(res => {
                      uni.hideLoading()
                      this.$u.toast(res.msg)
                      if(res.code == 1){
                          uni.navigateTo({
                              url: '/pages/mine/mineEnterPages/mineEnterResult/mineEnterResult?type=add'
                          })
                      }
                  })
              },
              previousStep(){
                  uni.pageScrollTo({
                      scrollTop: 0,
                      duration: 0
                  });
                  this.currentFormStep--;
                  this.currentApplyStep--;
              },
              _applyIndex() {
                  uni.showLoading()
                  MineCenterApi.applyIndex().then(res => {
                      uni.hideLoading()
                      this.flag = true
                      if(res.code == 1 && res.data.log && res.data.log.id){
						  this.id = res.data.log.id
                          this.log_data = res.data.log
                      }
                  })
              },
          }
      }
  </script>
  
  <style lang="scss" scoped></style>
  